package ICOM5016.Servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ICOM5016.DTO.Item;
import ICOM5016.Services.ItemService;

/**
 * Servlet implementation class ReadItemServlet
 */
@WebServlet("/ReadItemServlet")
public class ReadItemServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReadItemServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String forwardURL = null;
		
		String param1 = request.getParameter("searchQuery");
		//REMEMBER TO TOKENIZE QUERY
		ItemService itemService = ItemService.getNewInstance(); //return "new" item
		Item item = itemService.getItem(param1);
		
		if (item==null){
			forwardURL="./NoItemFound.jsp";
		}
		else{
			session.setAttribute("item", item);
			forwardURL="./Results.jsp";
		}
		
		RequestDispatcher dispatcher = request.getRequestDispatcher(forwardURL);
		dispatcher.forward(request, response);
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
